/**
 * 跑马灯组件
 */
<template>
  <div class="contain">
    <q-icon name="volume_up" size="25px" color="yellow" />
    <div class="box">
      <div class="merquee">
        <span class="merquee-txt text-yellow">{{ content }}</span>
      </div>
    </div>
  </div>
</template>

<script>
export default {
  props: {
    content: {
      type: String,
      default: '跑马灯'
    }
  },
  mounted () {
    this.redraw();
  },
  methods: {
    redraw () {
      this.$nextTick(() => {
        if (document.querySelector('.marquee')) {
          const containerWidth = document.querySelector('.marquee').offsetWidth; // 跑马灯容器宽度
          const boxWidth = document.querySelector('.marquee .marquee-txt').offsetWidth; // 跑马灯内容宽度
          const duration = (boxWidth * 2 + containerWidth) / 150 + 's'; // 动画时间 150px/s
          document.querySelector('.marquee .marquee-txt').style.cssText = 'animation-duration:' + duration;
        }
      });
    }
  },
  watch: {
    content () {
      this.redraw();
    }
  }
}
</script>

<style lang="scss" scoped>
/* 文字滚动 */
.contain {
  display: flex;
  width: 100%;
  align-items: center;
}

.box {
  display: flex;
  flex: 1;
  align-items: center;
}
.merquee {
  height: 25px;
  line-height: 25px;
  width: 100%;
  box-sizing: border-box;
  word-break: break-all;
  white-space: nowrap;
  overflow: hidden;
}
.merquee-txt {
  display: inline-block;
  padding-left: 100%;
  /* 从右至左开始滚动 */
  animation: marqueeTransform 12s linear infinite;
}

@keyframes marqueeTransform {
  0% {
    transform: translate(0, 0);
  }

  100% {
    transform: translate(-100%, 0);
  }
}
</style>
